package org.hit.burkun.dbimport;

import java.util.Collection;
import java.util.LinkedList;

import org.hit.burkun.db.DBHelper;
import org.hit.burkun.db.DBTypeConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MemoryImport {

	private String tableName;
	private int totalPropertyNum;
	private boolean hasAutoIncreasementId = true;
	private DBTypeConverter typeConverter;
	private DBHelper dbHelper;

	private int batchImportNum = 1000;
	private static Logger logger = LoggerFactory.getLogger(MemoryImport.class);

	public MemoryImport(String tableName, DBHelper dbHelper,
			boolean hasAutoIncreasementID, DBTypeConverter typeConverter) {
		this.tableName = tableName;
		this.dbHelper = dbHelper;
		this.totalPropertyNum = typeConverter.getTypes().length;
		this.hasAutoIncreasementId = hasAutoIncreasementID;
		this.typeConverter = (DBTypeConverter) typeConverter.clone();

	}

	public void setBatchImportNumber(int num) {
		this.batchImportNum = num;
	}

	/**
	 * 导入部分数据，且自动生成primary id, 返回当前的primary id
	 * 
	 * @param data
	 * @param curPid
	 * @return
	 */
	public int doImport(Collection<String[]> data, int curPid) {
		dbHelper.connectionDB();
		logger.debug("connect to db...");
		int counter = 0;
		int autoID = curPid;
		LinkedList<String[]> dump = new LinkedList<String[]>();
		for (String[] items : data) {
			String[] newItems = items;
			if (hasAutoIncreasementId) {
				newItems = new String[totalPropertyNum];
				for (int i = 0; i < items.length; i++) {
					newItems[i + 1] = items[i];
				}
				autoID += 1;
				newItems[0] = Integer.toString(autoID);
			}
			if (counter == batchImportNum) {
				logger.debug("insert " + batchImportNum + " item into db...");
				dbHelper.insertBatch(tableName, dump, typeConverter);
				dump.clear();
				dump.add(newItems);
				counter = 1;
			} else {
				dump.add(newItems);
				counter += 1;
			}
		}
		if (counter > 0) {
			logger.debug("insert " + counter + " item into db...");
			dbHelper.insertBatch(tableName, dump, typeConverter);
		}
		dbHelper.disconnection();
		return autoID;
	}

	/*
	 * 一次导入全部数据，如果要自动生成primary id，那么data应该包含所有数据
	 */
	public int doImport(Collection<String[]> data) {
		return doImport(data, 0);
	}

	/**
	 * 导入primary_id为空的值
	 * 
	 * @param data
	 * @param fileds
	 */
	public void doImport(Collection<String[]> data, String[] fileds) {
		// 设置称false
		hasAutoIncreasementId = false;
		dbHelper.connectionDB();
		logger.debug("connect to db...");
		int counter = 0;
		LinkedList<String[]> dump = new LinkedList<String[]>();
		for (String[] items : data) {
			String[] newItems = items;
			if (counter == batchImportNum) {
				logger.debug("insert " + batchImportNum + " item into db...");
				dbHelper.insertBatch(tableName, fileds, dump, typeConverter);
				dump.clear();
				dump.add(newItems);
				counter = 1;
			} else {
				dump.add(newItems);
				counter += 1;
			}
		}
		if (counter > 0) {
			logger.debug("insert " + counter + " item into db...");
			dbHelper.insertBatch(tableName, fileds, dump, typeConverter);
		}
		dbHelper.disconnection();
	}

}
